﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;

namespace CoolERP_Web.User
{
    public partial class ZL_CW_Query : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.AspNetPager1.PageSize = GridView1.PageSize;
                BindData();
            }
        }

        #region=======GridView处理=======
        private void BindData()
        {
            using (ZL_CWBLL cBLL = new ZL_CWBLL())
            {
                int RecordCount = 0;
                List<ZL_CW> itemLists = cBLL.GetPagerList(null, "CKID,QU,HANG,LIE,CENG,GE", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                this.AspNetPager1.RecordCount = RecordCount;
                GridView1.DataSource = itemLists;
                GridView1.DataBind();
                this.AspNetPager1.Visible = RecordCount > 0;
                panMsg.Visible = !this.AspNetPager1.Visible;
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Literal liCK = e.Row.FindControl("liCK") as Literal;
                ZL_CW cw = e.Row.DataItem as ZL_CW;
                if (liCK != null)
                    liCK.Text = cw.ZL_CK.MC;
                DropDownList ddlCK = e.Row.FindControl("ddlCK") as DropDownList;
                if (ddlCK != null)
                {
                    BindCK(ddlCK, "");
                    if (ddlCK.Items.FindByValue(cw.CKID.ToString()) != null)
                        ddlCK.SelectedValue = cw.CKID.ToString();
                }
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindData();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Guid ID = Guid.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            DropDownList ddlCK = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlCK");
            //TextBox txtBH = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBH");
            TextBox txtQU = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtQU");
            TextBox txHANG = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txHANG");
            TextBox txtLIE = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtLIE");
            TextBox txtCENG = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCENG");
            TextBox txtGE = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGE");
            using (ZL_CWBLL cBLL = new ZL_CWBLL())
            {
                ZL_CW item = cBLL.GetEntity(t => t.ID == ID);
                item.CKID = new Guid(ddlCK.SelectedValue);
                //item.BH = txtBH.Text.Trim();
                item.QU = txtQU.Text.Trim();
                item.HANG = txHANG.Text.Trim();
                item.LIE = txtLIE.Text.Trim();
                item.CENG = txtCENG.Text.Trim();
                item.GE = txtGE.Text.Trim();
                cBLL.Update(item);
            }
            GridView1.EditIndex = -1;
            BindData();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            using (ZL_CWBLL cBLL = new ZL_CWBLL())
            {
                cBLL.Deletes("ID", GridView1.DataKeys[e.RowIndex].Value.ToString());
            }
            GridView1.EditIndex = -1;
            BindData();
        }
        #endregion

        #region=======添加类型=======
        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            GridView1.ShowFooter = true;
            DropDownList ddlCK = GridView1.FooterRow.FindControl("ddlCK") as DropDownList;
            if (ddlCK != null)
                BindCK(ddlCK, "");
            BindData();
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            DropDownList ddlCK = GridView1.FooterRow.FindControl("ddlCK") as DropDownList;
            //TextBox txtBH = GridView1.FooterRow.FindControl("txtBH") as TextBox;
            TextBox txtQU = GridView1.FooterRow.FindControl("txtQU") as TextBox;
            TextBox txHANG = GridView1.FooterRow.FindControl("txHANG") as TextBox;
            TextBox txtLIE = GridView1.FooterRow.FindControl("txtLIE") as TextBox;
            TextBox txtCENG = GridView1.FooterRow.FindControl("txtCENG") as TextBox;
            TextBox txtGE = GridView1.FooterRow.FindControl("txtGE") as TextBox;
            using (ZL_CWBLL cBLL = new ZL_CWBLL())
            {
                ZL_CW item = new ZL_CW();
                item.ID = Guid.NewGuid();
                item.CKID = new Guid(ddlCK.SelectedValue);
                //item.BH = txtBH.Text.Trim();
                item.QU = txtQU.Text.Trim();
                item.HANG = txHANG.Text.Trim();
                item.LIE = txtLIE.Text.Trim();
                item.CENG = txtCENG.Text.Trim();
                item.GE = txtGE.Text.Trim();
                cBLL.Insert(item);
            }
            GridView1.ShowFooter = false;
            BindData();
        }

        protected void btnCancel_Click(object sender, ImageClickEventArgs e)
        {
            GridView1.ShowFooter = false;
            BindData();
        }
        #endregion
    }
}